/******************************************************************************
 * @File name   :      ResetPasswordDaoImpl.java
 *
 * @Author      :      chen shixiang
 *
 * @Date        :      2011-6-22
 *
 * @Copyright Notice: 
 * Copyright (c) 2014 DMS, Inc. All  Rights Reserved.
 * This software is published under the terms of the DMS Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2011-6-22 上午09:48:35        chen shixiang     1.0            Initial Version
 * 2011-9-16 上午11:48:35        He Hao     1.1            Change Method getUserCertificateType Hql
 * 2011-9-16 下午5:13:35          He Hao     1.2            Change Method getUserMobilePhone Hql
 * 2011-10-21 下午02:45:14        HeHao       1.3            Change Method verifyResetPasswordRequest
 *****************************************************************************/
package com.cap.bts.framework.ge.resetpassword.dao.impl;

import java.util.List;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.cap.bts.framework.common.dao.callback.QueryJpaCallback;
import com.cap.bts.framework.common.dao.impl.BaseDaoImpl;
import com.cap.bts.framework.ge.resetpassword.dao.IResetPasswordDao;
import com.cap.bts.framework.ge.resetpassword.dto.CertificateDto;
import com.cap.bts.framework.ge.resetpassword.dto.ResetPasswordDto;
import com.cap.bts.framework.usermgnt.constant.AccountStatus;
import com.cap.bts.framework.usermgnt.constant.EmployeeStatus;
import com.cap.bts.framework.usermgnt.constant.UserGroupType;
import com.cap.bts.framework.usermgnt.domain.User;

/**
 *
 */
@Repository("resetPasswordDao")
public class ResetPasswordDaoImpl extends BaseDaoImpl<User, Long> implements IResetPasswordDao {

    /**
     * @Date        :      2011-6-22
     * @param arg0
     */
    @Inject
    public ResetPasswordDaoImpl(@Value("com.cap.bts.framework.usermgnt.domain.User")Class<User> arg0) {
        super(arg0);
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2011-6-22
     * @see com.cap.bts.dms.ge.resetpassword.dao.IResetPasswordDao#getUserCertificateType(java.lang.String)
    **/
    @Override
    public CertificateDto getUserCertificateType(String userCode) {
//        String hql="select new com.cap.bts.dms.ge.resetpassword.dto.CertificateDto(ei.certificateType) from User userInfo,EmployeeInfo ei where userInfo.employeeId=ei.id and userInfo.userType="+UserGroupType.DEALER.getValue()+" and userInfo.userCode='"+userCode+"'";
//        List<CertificateDto> listCertificate = getJpaTemplate().executeFind(new QueryJpaCallback<CertificateDto>(hql));
//        if(listCertificate.size()>0){
//            return listCertificate.get(0);
//        }
        return null;
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2011-6-22
     * @see com.cap.bts.dms.ge.resetpassword.dao.IResetPasswordDao#getUserMobilePhone(java.lang.String)
    **/
    @Override
    public Long getUserMobilePhone(String userCode) {
        String hql="select ei.mobilePhone from User userInfo,EmployeeInfo ei where userInfo.employeeId=ei.id and userInfo.userCode='"+userCode+"'  and userInfo.userType="+UserGroupType.DEALER.getValue()+"";
        List<Long> listCertificate = getJpaTemplate().executeFind(new QueryJpaCallback<Long>(hql));
        Long mobilePhone=0L;
        if(listCertificate.size()>0){
            mobilePhone = listCertificate.get(0);
        }
        return mobilePhone;
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2011-6-22
     * @see com.cap.bts.dms.ge.resetpassword.dao.IResetPasswordDao#verifyResetPasswordRequest(com.cap.bts.dms.ge.resetpassword.dto.ResetPasswordDto)
    **/
    @Override
    public boolean verifyResetPasswordRequest(ResetPasswordDto resetPasswordDto) {
        String hql="select userInfo from User userInfo,EmployeeInfo ei where userInfo.employeeId=ei.id and userInfo.isLocked="+AccountStatus.UNLOCK.getValue()+" and ei.employmentStatusId in ("+EmployeeStatus.NORMAL+","+EmployeeStatus.PEND_TO_CHANGE+") and ei.certificateNo='"+resetPasswordDto.getCertificateCode()+"' and userInfo.userType="+UserGroupType.DEALER.getValue()+"";
        List<User> listUser = getListByQuery(hql);
        if(listUser.size()>0){
            return true;
        }
        return false;
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2015年7月16日
     * @see com.cap.bts.framework.ge.resetpassword.dao.IResetPasswordDao#getUserMobilePhoneFromDealer(java.lang.String)
    **/
    @Override
    public String getUserMobilePhoneFromDealer(String userCode) {
        String hql="select di.contactPhone from User userInfo,MasterDataDealerInfo di where userInfo.userCode=di.dealerNum and userInfo.userCode='"+userCode+"'  and userInfo.userType="+UserGroupType.DEALER.getValue()+"";
        List<String> listCertificate = getJpaTemplate().find(hql);
        String mobile="";
        if(listCertificate.size()>0){
            mobile = listCertificate.get(0);
        }
        return mobile;
    }

}
